//
//  CustomTextField.m
//  Router
//
//  Created by teki on 15/10/13.
//  Copyright © 2015年 dc. All rights reserved.
//

#import "CustomTextField.h"

@interface CustomTextField ()
@property (nonatomic, strong)NSArray *iconArray;
@property (nonatomic, strong)UIButton *showPasswordButton;

@end

@implementation CustomTextField

-(id)initWithType:(CustomTextFieldType)type target:(id)target FontSize:(int)fontSize{
    self=[super init];
    if (self) {
        _iconArray=@[@"land_phone",@"land_password"];
        self.translatesAutoresizingMaskIntoConstraints=NO;
        self.backgroundColor=[UIColor clearColor];
        self.layer.borderColor=[UIColor whiteColor].CGColor;
        self.layer.borderWidth=1.0;
        self.layer.cornerRadius=5.0f;
        self.layer.masksToBounds=YES;
        [self loadContentViewWithType:type target:target FontSize:fontSize];
    }
    return self;
}

- (instancetype)init
{
    self = [super init];
    if (self)
    {
//        [self initUI];
    }
    return self;
}


- (instancetype)initWithCoder:(NSCoder *)aDecoder
{
    self = [super initWithCoder:aDecoder];
    if (self) {
//        [self initUI];
    }
    return self;
}


- (void)drawRect:(CGRect)rect
{
//    [self initUI];
}

- (void)initUI
{
    _iconArray=@[@"land_phone",@"land_password"];
    self.translatesAutoresizingMaskIntoConstraints = NO;
    self.backgroundColor=[UIColor clearColor];
    self.layer.borderColor=[UIColor whiteColor].CGColor;
    self.layer.borderWidth=1.0;
    self.layer.cornerRadius=5.0f;
    self.layer.masksToBounds=YES;

    
    CGFloat pointX=13;
    
    UIImageView *iconImageView;
    if (_type== 0) {
        
    }else{
        iconImageView=[self addImgViewWithImageName:_iconArray[_type - 1]];
        [iconImageView makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(self).offset(pointX);
            make.centerY.equalTo(self);
            make.size.equalTo(iconImageView.image.size);
        }];
    }
    
    _textField=[self addTextFieldWithFontSize:_fontSize];
    if (_type== 2) {
        _textField.secureTextEntry = YES;
    }
    _textField.textColor=[UIColor whiteColor];
    [_textField makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self).offset(5);
        make.bottom.equalTo(self).offset(-5);
        if (_type==0 ) {
            make.left.equalTo(self).offset(pointX);
            make.right.equalTo(self).offset(-5);
        }else{
            make.left.equalTo(iconImageView.right).offset(5);
            make.right.equalTo(self).offset(-5);
        }
    }];
}

- (void)setType:(NSInteger)type
{
    _type = type;
}

- (void)setFontSize:(NSInteger)fontSize
{
    _fontSize = fontSize;

}


-(void)setPlaceholder:(NSString *)placeholder
{
    if (_placeholder!=placeholder) {
        _placeholder=placeholder;
        _textField.placeholder = placeholder;
    }
}


- (void)setAttributePlaceholder:(NSAttributedString *)attributePlaceholder
{
    if (_attributePlaceholder != attributePlaceholder) {
        _attributePlaceholder = attributePlaceholder;
        _textField.attributedPlaceholder = attributePlaceholder;
    }
}


- (void)setIsShowPasswordButton:(BOOL)isShowPasswordButton
{
    _isShowPasswordButton = isShowPasswordButton;
    _showPasswordButton = [UIButton buttonWithType:UIButtonTypeCustom];
    [_showPasswordButton addTarget:self action:@selector(showPasswordButtonPressed:) forControlEvents:UIControlEventTouchUpInside];
    UIImage *normalImage = [UIImage imageNamed:@"register_eye"];
    [_showPasswordButton setImage:normalImage forState:UIControlStateNormal];
    [_showPasswordButton setImage:[UIImage imageNamed:@"register_openeye"] forState:UIControlStateSelected];
    [self addSubview:_showPasswordButton];
    
    [_textField updateConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(self).offset(-(8+normalImage.size.width));/// 图片21  离右边距 15
    }];
    
    _showPasswordButton.translatesAutoresizingMaskIntoConstraints = NO;
    [_showPasswordButton makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(self).offset(-8);
        make.centerY.equalTo(self);
    }];
}

-(void)setStyleColor:(UIColor *)color{
    self.layer.borderColor=color.CGColor;
}


-(void)loadContentViewWithType:(CustomTextFieldType)type target:(id)target FontSize:(int)fontSize
{
    CGFloat pointX=13;
    
    UIImageView *iconImageView;
    if (type==CustomTextFieldTypeNone) {
        
    }else{
        iconImageView=[self addImgViewWithImageName:_iconArray[type]];
        [iconImageView makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(self).offset(pointX);
            make.centerY.equalTo(self);
            make.size.equalTo(iconImageView.image.size);
        }];
    }
    
    _textField=[self addTextFieldWithTarget:target FontSize:fontSize];
    if (type==CustomTextFieldTypePwd) {
        _textField.secureTextEntry = YES;
    }
//    _textField.tintColor=RGB(202, 201, 206);
    _textField.textColor=[UIColor whiteColor];
    [_textField makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self).offset(5);
        make.bottom.equalTo(self).offset(-5);
        if (type==CustomTextFieldTypeNone) {
            make.left.equalTo(self).offset(pointX);
            make.right.equalTo(self).offset(-5);
        }else{
            make.left.equalTo(iconImageView.right).offset(5);
            make.right.equalTo(self).offset(-5);
        }
    }];
}

-(BOOL)resignFirstResponder{
    [super resignFirstResponder];
    [_textField resignFirstResponder];
    return YES;
}




- (void)showPasswordButtonPressed:(UIButton *)btn
{
    btn.selected = !btn.selected;
    _textField.secureTextEntry = !btn.selected;
}



#pragma mark -
#pragma mark   private Methods
-(UIImageView *)addImgViewWithImageName:(NSString *)imageName{
    UIImageView *imageView=UIImageView.new;
    imageView.image=[UIImage imageNamed:imageName];
    [self addSubview:imageView];
    return imageView;
}

-(UITextField *)addTextFieldWithTarget:(id)target FontSize:(int)fontSize{
    UITextField *textField = [[UITextField alloc]init];
    textField.translatesAutoresizingMaskIntoConstraints=NO;
    textField.delegate = target;
    textField.borderStyle = UITextBorderStyleNone;
    textField. autocapitalizationType = UITextAutocapitalizationTypeNone;
    textField.font=[UIFont systemFontOfSize:fontSize];
    textField.clearButtonMode = UITextFieldViewModeWhileEditing;
    textField.backgroundColor = [UIColor clearColor];
    textField.textColor = [UIColor whiteColor];
    [self addSubview:textField];
    return textField;
}

-(UITextField *)addTextFieldWithFontSize:(NSInteger)fontSize{
    UITextField *textField = [[UITextField alloc]init];
    textField.translatesAutoresizingMaskIntoConstraints=NO;
    textField.borderStyle = UITextBorderStyleNone;
    textField. autocapitalizationType = UITextAutocapitalizationTypeNone;
    textField.font=[UIFont systemFontOfSize:fontSize];
    textField.clearButtonMode = UITextFieldViewModeWhileEditing;
    textField.backgroundColor = [UIColor clearColor];
    textField.textColor = [UIColor whiteColor];
    [self addSubview:textField];
    return textField;
}
@end
